

clear
set more off
cap log close

***************************************************************************************************
* Program: figure3.do
* Purpose: Produce Figure 3
***************************************************************************************************

************************************************************************
** set macros (directory)
************************************************************************
global prison_dir "~/Dropbox/Prison_Covid/PNAS_Nexus_Replication"
global raw_data "${prison_dir}/raw_data"
global intermediate_data "${prison_dir}/intermediate_data"
global code "${prison_dir}/code"
global analysis_data "${prison_dir}/analysis_data"
global output "${prison_dir}/output"

************************************************************************
* Main Code
************************************************************************

import delimited "${raw_data}/CDCR_covid_19.csv", clear

* rename name to match prison boundaries data
gen NAME = upper(institutionname)
replace NAME = "AVENAL STATE PRISON" if NAME == "AVENAL STATE PRISON (ASP)"
replace NAME = "CALIFORNIA CITY CORRECTIONAL CENTER (CAC)" if NAME == "CALIFORNIA CITY CORRECTIONAL FACILITY (CAC)"
replace NAME = "CENTINELA STATE PRISON (CEN)" if NAME == "CALIFORNIA STATE PRISON-CENTINELA (CEN)"
replace NAME = "CALIFORNIA STATE PRISON, CORCORAN (COR)" if NAME == "CALIFORNIA STATE PRISON-CORCORAN (COR)"
replace NAME = "CALIFORNIA STATE PRISON, SACRAMENTO (SAC)" if NAME == "CALIFORNIA STATE PRISON-SACRAMENTO (SAC)"
replace NAME = "CALIFORNIA STATE PRISON, SOLANO (SOL)" if NAME == "CALIFORNIA STATE PRISON-SOLANO (SOL)"
replace NAME = "CENTRAL CALIFORNIA WOMEN'S FACILITY (CCFW)" if NAME == "CENTRAL CALIFORNIA WOMEN'S FACILITY (CCWF)"
replace NAME = "PLEASANT VALLEY STATE PRISON(PVSP)" if NAME == "PLEASANT VALLEY STATE PRISON (PVSP)"
replace NAME = "R J DONOVAN CORRECTIONAL FACILITY (RJD)" if NAME == "RICHARD J. DONOVAN CORRECTIONAL FACILITY (RJD)"
replace NAME = "CA SUBSTANCE ABUSE TREATMENT FACILITY (SATF)" if NAME == "SUBSTANCE ABUSE TREATMENT FACILITY (SATF)"

* get prison list
merge m:1 NAME using "${raw_data}/Prison_Boundaries.dta", keepusing(NAME ANALYTICSAMPLE) keep(match) nogenerate
drop if ANALYTICSAMPLE >=3 & ANALYTICSAMPLE !=. 

* merge to get prison's population
merge m:1 NAME using "${intermediate_data}/cdcr_population_w_ShapefileID.dta", ///
	keepusing(population_felons_201912) keep(match) nogenerate

* generate date
gen Stata_Date_local = date(date, "YMD")
format Stata_Date_local %td

gen year = year(Stata_Date_local)
gen month = month(Stata_Date_local)
gen week = week(Stata_Date_local)

* only keep the last day for each week 
sort NAME Stata_Date_local
drop if NAME == NAME[_n+1] & week == week[_n+1]
gen prison_new_cases_per1k = (totalconfirmed - totalconfirmed[_n-1]) / population_felons_201912 * 1000 ///
	if NAME == NAME[_n-1] & week == week[_n-1] + 1
replace prison_new_cases_per1k = totalconfirmed / population_felons_201912 * 1000 if month == 3 & year == 2020 & missing(prison_new_cases_per1k)
	
gen prison_new_deaths = (totaldeaths - totaldeaths[_n-1]) if NAME == NAME[_n-1] & week == week[_n-1] + 1
gen prison_new_deaths_per1k = prison_new_deaths / population_felons_201912 * 1000

* get proper name
gen proper_name = proper(NAME)
split proper_name, parse("(") generate(short_name)
replace short_name1 = trim(short_name1)
replace short_name1 = subinstr(short_name1, "'S", "'s", .)
drop proper_name

keep if year == 2020 & month <= 10 

* produce graph 
egen prison_id = group(NAME)
sort prison_id Stata_Date_local

tw (connected prison_new_cases_per1k Stata_Date_local if prison_id == 1, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 2, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 3, lcolor(gs13) mcolor(gs13)) ///	
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 4, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 5, lcolor(gs13) mcolor(gs13)) ///	
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 6, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 7, lcolor(gs13) mcolor(gs13)) ///	
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 8, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 9, lcolor(gs13) mcolor(gs13)) ///	
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 10, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 11, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 12, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 13, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 14, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 15, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 16, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 17, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 18, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 19, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 20, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 21, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 22, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 23, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 24, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 25, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 26, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 27, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 28, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 29, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 30, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 31, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 32, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 33, lcolor(gs13) mcolor(gs13)) ///		
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 34, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if prison_id == 35, lcolor(gs13) mcolor(gs13)) ///
	(connected prison_new_cases_per1k Stata_Date_local if NAME == "CALIFORNIA INSTITUTION FOR MEN (CIM)", lcolor(blue) mcolor(blue)) ///
	(connected prison_new_cases_per1k Stata_Date_local if NAME == "SAN QUENTIN STATE PRISON (SQ)", lcolor(red) mcolor(red) ///
	ytitle("New Cases Per 1,000") xtitle("Date") xline(22061, lpattern(dash) lcolor(orange)) ///
	 xlabel(`=daily("3/1/2020", "MDY")'  ///
		   `=daily("5/1/2020", "MDY")'  ///
		   `=daily("7/1/2020", "MDY")'  ///
		   `=daily("9/1/2020", "MDY")'  /// 
		   `=daily("11/1/2020", "MDY")', ///
		   format(%tdnn/dd/CCYY)) ///
	legend(order(36 "California Institution for Men" 37 "San Quentin State Prison")) )
graph export "${output}/figure3.png", replace 
